#include "editormanager.h"
#include "abstracteditor.h"
#include "../application.h"
#include "../mainwindow.h"
#include <QStackedWidget>

EditorManager::EditorManager(QObject *parent) :
    QObject(parent)
{
}

bool EditorManager::registerEditor(QString editorName, QPointer<AbstractEditor> editor)
{
    if(m_editors.find(editorName) != m_editors.end())
    {
        return false;
    }

    m_editors[editorName] = editor;
    editor->init();

    QPointer<QStackedWidget> sw = Application::app().mainWindow()->editorsStackedWidget();
    editor->m_nIndexInStack = sw->addWidget(editor->m_view);

    return true;
}

bool EditorManager::setActiveEditor(QString editorName)
{
    if(m_editors.find(editorName) == m_editors.end())
    {
        return false;
    }

    if(m_activeEditor)
    {
        m_activeEditor->deactivate();
    }

    m_activeEditor = m_editors[editorName];

    if(m_activeEditor)
    {
        m_activeEditor->activate();
    }

    return true;
}

QPointer<AbstractEditor> EditorManager::activeEditor() const
{
    return m_activeEditor;
}
